﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DLL;
using NursingFormSystem.Model;
using SQLDLL;
using System;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace NursingFormSystem
{
    public partial class HealthEducationManger : Form
    {
        public static Tb_User LoginUser;
        public string select = "";
        public string departmentid = "";
        public HealthEducationManger(string[] UserParam)
        {
            InitializeComponent();
            Text = "护理记录单修改";
            selectlist();
        }
        void selectlist()
        {
            select = @"SELECT *   FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduDiseases](NOLOCK) where 1=1 ";
            if (departmentid != null && departmentid != "")
            {
                string[] temp = Regex.Split(departmentid, ",", RegexOptions.IgnoreCase);
                if (temp.Length == 1)
                {
                    select += "and DepartmentID like '%" + temp[0] + "%'";
                }
                if (temp.Length > 1)
                {
                    select += "and DepartmentID like '%" + temp[0] + "%'";
                    for (int i = 1; i < temp.Length; i++)
                    {
                        select += @" or  DepartmentID like '%" + temp[i] + "%'";
                    }
                }

            }
            if (textEdit1.Text.Length > 0)
            {
                select += "and TemplateName like '%" + textEdit1.Text + "%'";
            }
            select += "order by OrderNum";
            DataSet Temp = SqlHelper.ExecuteDataset(select, CommandType.Text);
            DataTable HealthEducationList = Temp.Tables[0];
            HealthEducationList.Columns.Add("勾选", Type.GetType("System.Boolean"));
            HealthEducationList.Columns["勾选"].SetOrdinal(0);
            HealthEducationList.Columns["TemplateName"].SetOrdinal(1);
            HealthEducationList.Columns["DepartmentName"].SetOrdinal(2);
            gridControl1.DataSource = HealthEducationList;
            GridOptimization();
        }
        public Tb_User InitLoginUser(string[] UserParam)
        {
            //if (LoginUser == null)
            //{
            LoginUser = new Tb_User();
            LoginUser.UserId = UserParam[0];
            LoginUser.UserName = UserParam[1];
            LoginUser.DepartmentCode = UserParam[2];
            LoginUser.DepartmentName = UserParam[3];
            LoginUser.JobTitle = UserParam[4];
            LoginUser.PositionTitle = UserParam[5];
            //}
            return LoginUser;
        }

        RepositoryItemCheckEdit CheckItem = new RepositoryItemCheckEdit();
        const string gcCheckFieldName = "勾选";
        void GridOptimization()
        {
            gridView1.OptionsCustomization.AllowSort = false;
            gridView1.OptionsView.ShowGroupPanel = false;
            gridView1.OptionsBehavior.Editable = true;
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.OptionsCustomization.AllowColumnMoving = false;
            gridView1.Columns["id"].Visible = false;
            gridView1.Columns["DiseasesGuid"].Visible = false;
            gridView1.Columns["OrderNum"].Visible = false;
            gridView1.Columns["DiseasesName"].Caption = "病种";
            gridView1.Columns["DepartmentName"].Caption = "科室";
            gridView1.Columns["TemplateName"].Caption = "表单名称";
            gridView1.Columns["DiseasesName"].Width = 70;
            gridView1.Columns["DepartmentName"].Width = 200;
            gridView1.Columns["TemplateName"].Width = 180;
            gridView1.Columns["DepartmentID"].Visible = false;
            gridView1.CustomDrawColumnHeader += gridView1_CustomDrawColumnHeader;
            gridView1.OptionsSelection.MultiSelect = true;
            gridView1.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect;
            gridView1.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            gridView1.HorzScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Always;
            gridView1.VertScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Always;
            gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;   //是否显示过滤面板
            gridView1.OptionsCustomization.AllowFilter = false;                      //是否允许过滤        
            gridView1.OptionsCustomization.AllowColumnMoving = false;
            gridView1.Columns[0].Caption = " ";
            gridView1.Columns["勾选"].Width = 30;
            for (int i = 0; i < gridView1.Columns.Count; i++)
            {
                if (gridView1.Columns[i].Name != "col勾选")
                {
                    gridView1.Columns[i].OptionsColumn.AllowEdit = false;
                }
            }
        }

        void gridView1_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
        {
            GridView _view = sender as GridView;
            Draw.DrawHeaderCheckBox(gridView1, CheckItem, gcCheckFieldName, e);
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            Point pt = gridControl1.PointToClient(MousePosition);
            GridHitInfo info = gridView1.CalcHitInfo(pt);
            if (info.Column == null)
            {
                return;
            }
            if (!info.InRowCell && info.Column.AbsoluteIndex == 0)//Index判断用于哪一列的列头
            {
                string value;
                int Checked = 0;
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    value = gridView1.GetDataRow(i)["勾选"].ToString();
                    if (value == "True")
                    {
                        Checked++;
                    }
                }
                if (Checked == 0)
                {
                    for (int i = 0; i < gridView1.RowCount; i++)
                    {
                        gridView1.SetRowCellValue(i, gridView1.Columns[0], true);
                    }
                }
                else
                {
                    for (int i = 0; i < gridView1.RowCount; i++)
                    {
                        gridView1.SetRowCellValue(i, gridView1.Columns[0], false);
                    }
                }
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            string value = "";
            int Checked = 0;
            string id = "";
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                value = gridView1.GetDataRow(i)["勾选"].ToString();
                if (value == "True")
                {
                    Checked++;
                    if (Checked > 1) break;
                    id = gridView1.GetDataRow(i)["DiseasesGuid"].ToString();
                }
            }
            if (Checked == 0)
            {
                XtraMessageBox.Show("没有勾选任何条目");
                return;
            }
            if (Checked > 1)
            {
                XtraMessageBox.Show("目前只支持单独一条宣教单的修改");
                return;
            }
            SetEduDiseases F2 = new SetEduDiseases(id, departmentid);
            F2.ShowDialog(this);
            if (F2.DialogResult == DialogResult.OK)
            {
                simpleButton2_Click(null, null);
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            selectlist();
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            string value = "";
            int Checked = 0;
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                value = gridView1.GetDataRow(i)["勾选"].ToString();
                if (value == "True")
                {
                    Checked++;
                }
            }
            if (Checked == 0)
            {
                XtraMessageBox.Show("没有勾选任何条目");
                return;
            }
            DialogResult Delete;
            Delete = XtraMessageBox.Show("确定要删除勾选的" + Checked + "条表单吗吗？", "删除询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (Delete == DialogResult.OK)
            {
                string[] id = new string[Checked];

                for (int i = 0, j = 0; i < gridView1.RowCount; i++)
                {
                    value = gridView1.GetDataRow(i)["勾选"].ToString();
                    if (value == "True")
                    {
                        id[j] = gridView1.GetDataRow(i)["id"].ToString();
                        j++;
                    }
                }
                string sql = "";
                string DiseasesGuid = "";
                for (int i = 0; i < Checked; i++)
                {
                    sql = @"SELECT DiseasesGuid  FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduDiseases](NOLOCK) where id = '" + id[i] + "'";
                    object temp = SqlHelper.ExecuteScalar(CommandType.Text, sql);
                    DiseasesGuid = temp.ToString();
                    sql = @"SELECT ClassGuid   FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduClass](NOLOCK) where DiseasesGuid = '" + DiseasesGuid + "'";
                    DataTable Class = SqlHelper.ExecuteDataset(sql, CommandType.Text).Tables[0];
                    for (int j = 0; j < Class.Rows.Count; j++)
                    {
                        string classguid = Class.Rows[j]["ClassGuid"].ToString();
                        sql = @"SELECT *  FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduProject](NOLOCK) where ClassGuid ='" + classguid + "'";
                        DataTable temp2 = SqlHelper.ExecuteDataset(sql, CommandType.Text).Tables[0];
                        for (int k = 0; k < temp2.Rows.Count; k++)
                        {
                            string projectid = temp2.Rows[k]["ProjectGuid"].ToString();
                            sql = @" delete from [BNNursingDocsSystem_Develop].[dbo].[HealthEduProject] where ProjectGuid ='" + projectid + "'";
                            SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                            sql = @"delete  FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduContent] where ProjectGuid ='" + projectid + "'";
                            SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                        }
                        sql = @" delete from [BNNursingDocsSystem_Develop].[dbo].[HealthEduClass] where ClassGuid ='" + classguid + "'";
                        SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                    }

                    sql = @"delete from [BNNursingDocsSystem_Develop].[dbo].[HealthEduDiseases] where  id= " + id[i] + "";
                    SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                }
                XtraMessageBox.Show(Checked + "条数据被更新");
                simpleButton2_Click(null, null);
            }
            else return;
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            if (departmentid == "")
            {
                XtraMessageBox.Show("未选择相应病区，点击确定后讲跳转至选择病区页面！");
            }
            SetEduDepartment F2 = new SetEduDepartment(departmentid);
            F2.ShowDialog(this);
            if (F2.DialogResult == DialogResult.OK)
            {
                departmentid = F2.choose;
                SetEduDiseases F3 = new SetEduDiseases(null, departmentid);
                F3.ShowDialog();
                if (F3.DialogResult == DialogResult.OK)
                {
                    XtraMessageBox.Show("新增表单成功");
                    simpleButton2_Click(null, null);
                }
            }
            else
            {
                departmentid = null;
                XtraMessageBox.Show("未选择相应病区，新增表单失败！");
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (departmentid == null)
            {
                departmentid = "";
            }
            if (departmentid.Length > 0)
            {
                SetEduDepartment F2 = new SetEduDepartment(departmentid);
                F2.ShowDialog(this);
                if (F2.DialogResult == DialogResult.OK)
                {
                    departmentid = F2.choose;
                    simpleButton2_Click(null, null);
                }
                else
                {
                    departmentid = null;
                }
            }
            else
            {
                SetEduDepartment F2 = new SetEduDepartment(null);
                F2.ShowDialog(this);
                if (F2.DialogResult == DialogResult.OK)
                {
                    departmentid = F2.choose;
                    simpleButton2_Click(null, null);
                }
                else
                {
                    departmentid = null;
                }
            }
        }
    }
}